﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Common;
using System.Text;
public partial class website_admin_insurance_productitemeditor : PageBase
{
    protected string pageTitle = "新增";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        if (RequestBase.GetFormString("__EVENTTARGET") == "saveinfo")
        {
            SaveInfo(RequestBase.GetFormString("__EVENTARGUMENT"));
        }

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadProduct":
                ProductDataBind();
                break;
        }

        if (!IsPostBack)
        {
            ProductDataBind();
            if (RequestBase.GetQueryString("otype") == "modify")
            {
                pageTitle = "编辑";
                InitializeObj();
            }
            else
            {
                selStatusCode.Disabled = false;
            }
        }
    }
  
    #region 类别列表数据绑定
    void ProductDataBind()
    {
        StringBuilder SQL = new StringBuilder().Append(" AND DeletionStateCode = 0");
        if (Utils.IsNumber(RequestBase.GetQueryString("typecode")))
            SQL.Append(" AND TypeCode = " + RequestBase.GetQueryString("typecode"));

        DataTable dt = BLL.CommonBLL.Select("InsuranceProductBase", "*", SQL.ToString() + " ORDER BY LineNum ASC"); 
        DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);

        if (dt != null && dt.Rows.Count > 0 && Utils.IsNumber(RequestBase.GetQueryString("productnumber")))
        {
            if (dt.Select("ProductNumber = '" + RequestBase.GetQueryString("productnumber") + "'").Length > 0)
            {
                selProductList.Value = dt.Select("ProductNumber = '" + RequestBase.GetQueryString("productnumber") + "'")[0]["ProductId"].ToString();
            }
        }
        else if (Utils.IsGuid(RequestBase.GetQueryString("productid")))
        {
            selProductList.Value = RequestBase.GetQueryString("productid");
        }
    }
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList();
        ListItem li;

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            li = new ListItem("\t" + str + "\t" + d["ProductName"].ToString(), d["ProductId"].ToString());

            selProductList.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, d["ProductId"].ToString().Trim(), dt);
        }
    }
    #endregion 

    #region 获取产品明细实体
    /// <summary>
    /// 获取信息实体
    /// </summary>
    void InitializeObj()
    {
        ProductItemObj = BLL.Insurance.InsuranceProductItemBase.GetProductItemObjectById(RequestBase.GetQueryString("productitemid"));
    }

    /// <summary>
    /// 产品明细信息实体
    /// </summary>
    Model.Insurance.InsuranceProductItemBase ProductItemObj
    {
        get
        {
            Model.Insurance.InsuranceProductItemBase _obj = new Model.Insurance.InsuranceProductItemBase();
            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("productitemid")))
                _obj.ProductItemId = RequestBase.GetQueryString("productitemid");

            _obj.ProductItemName = txtProductItemName.Value;
            _obj.ProductId = selProductList.Value;
            _obj.ProtectedDays = int.Parse(txtDays.Value);
            _obj.SalesPrice = decimal.Parse(txtPrice.Value);
            _obj.StatusCode = int.Parse(selStatusCode.Value);
            _obj.Integral = int.Parse(txtIntegral.Value);

            return _obj;
        }
        set
        {
            if (value != null)
            {
                txtProductItemName.Value = value.ProductItemName;
                selProductList.Value = value.ProductId;
                txtDays.Value = value.ProtectedDays.ToString();
                txtPrice.Value = string.Format("{0:f2}", value.SalesPrice);
                selStatusCode.Value = value.StatusCode.ToString();
                txtIntegral.Value = value.Integral.ToString();
            }
        }
    }
    #endregion

    #region 数据处理
    /// <summary>
    /// 数据处理
    /// </summary>
    void SaveInfo(string omode)
    { 
        string result = "";

        if (RequestBase.GetQueryString("otype") == "add")
        {
            result = BLL.Insurance.InsuranceProductItemBase.ProductItem_Create(ProductItemObj); 
        }
        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            result = BLL.Insurance.InsuranceProductItemBase.ProductItem_Modify(ProductItemObj); 
        }

        if (result.IndexOf("succeeded") >= 0)
        {
            if (omode == "saveandclose")
                JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.LoadingData(); window.close();");
            else
                if (Utils.IsNumber(RequestBase.GetQueryString("typecode")))
                    JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.document.location.reload(); window.location.href='productitemeditor.aspx?otype=add&productid=" + selProductList.Value.Trim() + "&typecode=" + RequestBase.GetQueryString("typecode") + "';");
                else
                    JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.LoadingData(); window.location.href='productitemeditor.aspx?otype=add&productid=" + selProductList.Value.Trim() + "';");
        }
        else
        {
            this.Page.RegisterStartupScript("writejs", "<script>alert('" + result + "');</script>");
            return;
        }
    }
    #endregion
}
